class_name CharacterEntity
extends CharacterBody2D

##获取 entity_data
##[codeblock]
##var data = entity.get_entity_data()
##[/codeblock]
##获取 component
##[codeblock]
##var component = entity.get_component_by_name("")
##[/codeblock]
@export var entity_data:EntityData
@export var sprite2d:Sprite2D
@export var animation_player:AnimationPlayer
@onready var physics_body_collision:CollisionShape2D = %PhysicsBodyCollision
@onready var navigation_agent_2d: NavigationAgent2D = %NavigationAgent2D
var logic_body_area:Area2D
var blackboard_data:BlackboardData
var component_dict:Dictionary
var interact_items:Array[InteractiveItem] = []

func init():
	pass
func get_entity_data() -> EntityData:
	return entity_data
func get_blackboard_data(key:String):
	return blackboard_data.get_data(key)
func set_blackboard_data(key:String,value):
	blackboard_data.set_data(key,value)
func get_blackboard_data_dict() -> Dictionary:
	return blackboard_data.get_data_dict()
func clear_blackboard_data_dict():
	blackboard_data.clear_data_dict()
func get_components() -> Array[BaseComponent]:
	var components:Array[BaseComponent] = []
	for child in get_children():
		if child is BaseComponent:
			components.append(child)
	components.sort_custom(func (a,b):return a.priority < b.priority)
	return components
func get_component_dict() -> Dictionary:
	return component_dict
func get_component_by_name(component_name:String) -> BaseComponent:
	if component_dict.has(component_name):
		return component_dict[component_name]
	return null
func get_pivot() -> Vector2:
	return Vector2.ZERO
func get_vel() -> Vector2:
	return velocity
func get_max_vel() -> float:
	return get_entity_data().max_speed
func get_pos() -> Vector2:
	return global_position
func get_mass() -> float:
	return 0
func get_valid_poses() -> Array[Vector2i]:
	return []
func get_valid_poses_by_layer_mask(layer_mask) -> Array[Vector2i]:
	return []
func get_view() -> bool:
	return get_entity_data().is_sky_view
func get_detector(detector_type:GameEnum.DetectorType) -> Detector:
	var detector_component:DetectComponent = get_component_by_name("DetectComponent")
	if not detector_component:
		return null
	return detector_component.get_detector(detector_type)
func clear_all_interactive_item_selected():
	pass

func _init() -> void:
	z_index = StaticValue.Z_INDEX_ENTITY
	blackboard_data = BlackboardData.new()
	component_dict = {}
func _ready() -> void:
	entity_data = get_entity_data()
	for child in get_components():
		component_dict[child.component_name] = child
		child.init_component(self)
	logic_body_area = get_component_by_name("SlotComponent").get_node("%LogicBodyArea")
	navigation_agent_2d.navigation_layers = entity_data.layer_navigation
	entity_data.data_changed.connect(func(tag):
		if tag == "layer_navigation":
			navigation_agent_2d.navigation_layers = entity_data.layer_navigation
		)
func _process(delta: float) -> void:
	for i in range(interact_items.size()):
		interact_items[i].set_seleted(i == 0)
